package com.tenton.service.Impl;

import com.tenton.dao.AdminDao;
import com.tenton.pojo.Admin;
import com.tenton.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Date: 2021/2/2
 * @Author: Tenton
 * @Description:
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminDao adminDao;

    /**
     * 获取所有管理员信息
     * @return
     */
    @Override
    public List<Admin> listAdmin() {
        return adminDao.findAll();
    }

    /**
     * 登录判断
     * @param name
     * @param password
     * @param model
     * @param session
     * @return
     */
    @Override
    public boolean login(String name, String password, Model model, HttpSession session) {
        //判断用户名和密码是否正确
        boolean flag = false;
        List<Admin> admins = adminDao.findAll();
        for (Admin admin : admins) {
            if (admin.getName().equals(name) && admin.getPassword().equals(password)){
                //将登录名和Id保存到session域中
                session.setAttribute("loginAdmin",name);
                session.setAttribute("adminId",admin.getId());
                flag = true;
                break;
            }
        }
        return flag;
    }

}
